package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;


//未登录下的经验搜索
public class searchExperience extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String key = request.getParameter("experience");

        String sql = "SELECT * FROM `courses` WHERE" + " `course` LIKE '%" + key + "%'" +
                " OR `nam` LIKE '%" + key + "%'";
        System.out.println(sql);


        try {
            List<Experience> experiences = DBMS.getExperience(sql);
            response.setCharacterEncoding("UTF-8");
            Writer writer = response.getWriter();
            StringBuilder sb = new StringBuilder();

            sb.append(NoLoginPage.getHeader());
            sb.append(toHtml(experiences));
            sb.append(NoLoginPage.getEnd());
            writer.write(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private String toHtml(List<Experience> experiences) {
        StringBuilder sb = new StringBuilder();
        sb.append("<style type=\"text/css\">\n" +
                "\tbody{\n" +
                "\t\tbackground: url(\"./pic/beijing.jpg\") no-repeat center center fixed;\n" +
                "                /*兼容浏览器版本*/\n" +
                "                -webkit-background-size: cover;\n" +
                "                -o-background-size: cover;\n" +
                "                background-size: cover;\n" +
                "\t}\n" +
                "</style>");

//        sb.append("<center><br><table style='width:60%' border='0' cellpadding='10'>");

//        <tr><td>学号:</td><td><input type='text' name=numbb value=''></td></tr>
//        sb.append("<tr style='background-color:#336699;color:#ff5400'>" +
//                "<th style=\"visibility:hidden\">编号</th><th>名字</th><th style=\"visibility:hidden\"></th>"
//                +"<th></th><th>科目</th><><th>建议</th>"
//                + "<th></th><th></th></tr>");

        for (int i = 0; i < experiences.size(); i++) {
            Experience experience = experiences.get(i);
//            if (i % 2 == 0) {
//                sb.append("<tr style='background-color:#20b2aa'>");
//            } else {
//                sb.append("<tr style='background-color:#7ab8cc'>");
//            }
//
            sb.append("<center>");
            sb.append("<font  face=\"宋体\" size=\"4\" >提供者：</font>")
                    .append("<font  face=\"宋体\" size=\"4\" >")
                    .append(experience.getName()).append("</font>");

            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");

            sb.append("<font  face=\"宋体\" size=\"4\" >科目：</font>")
                    .append("<font  face=\"宋体\" size=\"4\" >")
                    .append(experience.getCourse()).append("</font>");

            sb.append("<br>");

            sb.append("<font  face=\"宋体\" size=\"4\" color=\"#003153\">建议</font>");
            sb.append("<br>");
            sb.append("<textarea name=\"feel\"  cols=\"40\" rows=\"10\" >")
                    .append(experience.getAdvice()).append("</textarea>");

            sb.append("<br>");
            sb.append("<br>");




//     <font style='visibility:hidden' face="宋体" size="2" color="#003153">搜索同学们给出的具体学科的相关学习建议</font><br/>

//            sb.append("<font style='visibility:hidden' face=\"宋体\" size=\"2\" color=\"#003153\">").append(experience.getID()).append("</td>");
//            sb.append("<td>").append(experience.getName()).append("</td>");
//            sb.append("<th style='visibility:hidden'></th>");
//            sb.append("<th style='visibility:hidden'></th>");
//            sb.append("<td>").append(experience.getCourse()).append("</td>");
//            sb.append("<br>");
//            sb.append("<br>");
//            sb.append("<br>");

//            <tr><td>描述: </td><td><textarea name="feel" cols="50" rows="5"></textarea></td></tr>
//            sb.append("<tr><td><textarea name='advice' cols='60' rows='10'>").append(experience.getAdvice())
//                    .append("</textarea></td></tr>");
//            sb.append("<br>");
//            sb.append("<br>");
//            sb.append("<br>");

//            sb.append("<td>").append(
//                    String.format("<a href='./update?id=%s'>修改</a>", experience.getID())
//            ).append("</td>");
//            sb.append("<td>").append(
////                    String.format("<a href='./delete?id=%s'>删除</a>", experience.getID())
//            ).append("</td>");
//            sb.append("</tr>");
        }
//        sb.append("</table></center>");
        sb.append("</center>");
        return sb.toString();
    }
}









//
//        try {
//            List<Experience> experiences = DBMS.getExperience(sql);
//
//            response.setContentType("application/json");
//            response.setCharacterEncoding("UTF-8");
//            try(Writer writer = response.getWriter()) {
//                writer.write(this.toJson(experiences));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private String toJson(List<Experience> experiences) {
//        StringBuilder sb = new StringBuilder();
//        sb.append("{");
//        sb.append("\"experiences\": [");
//
//        for(int i=0; i<experiences.size(); i++) {
//            Experience experience = experiences.get(i);
//            if (i > 0) sb.append(",");
//            sb.append(String.format(
//                    "{\"id\": %s, \"nam\": \"%s\",\"course\": \"%s\",\"advice\": %s}",
//                    experience.getID(), experience.getName(), experience.getCourse(), experience.getAdvice()
//            ));
//        }
//
//        sb.append("]");
//        sb.append("}");
//        return sb.toString();
//    }
//}